/**
 * @author Albrecht Senden
 */

var win = Ti.UI.currentWindow;
var json = '';
var data = [];
var tableView;
win.showNavBar();

tableView = Titanium.UI.createTableView({
	//data:data,
	//filterAttribute:'filter',
	backgroundColor:'white'
});

tableView.addEventListener('click', function(e)
	{
		var rowNum = e.index;
		Ti.API.info('table view row clicked - source ' + e.source + 'in row' + rowNum+ '>');
		var TestName = JSON.stringify(e.row);
		Ti.API.info('test >>' + TestName + e.row.HotelName);
		
		var detailwin = Titanium.UI.createWindow({
        url:'detailview.js',
        title:'Details',
        hideNavBar:false
    	});
    
    detailwin.HotelName = e.row.HotelName;
    detailwin.HotelImg = e.row.HotelImg
	detailwin.open({modal:true,modalTransitionStyle:Ti.UI.iPhone.MODAL_TRANSITION_STYLE_PARTIAL_CURL,fontSize:12,modalStyle:Ti.UI.iPhone.MODAL_PRESENTATION_FULLSCREEN,fontSize:12,navBarHidden:true});	
		// use rowNum property on object to get row number
		//var updateRow = createUpdateRow('You clicked on the '+e.source.clickName);
		//tableView.updateRow(rowNum,updateRow,{animationStyle:Titanium.UI.iPhone.RowAnimationStyle.LEFT});
	});

win.add(tableView);

//// when win opens >focus< the eventlistener cicks in

win.addEventListener('open', function() 
{ 
	Ti.API.info('fired open event');
	json = JSON.parse(win.resultsText);
	parse(json);
	
});
 
win.addEventListener('blur', function() 
{ 
	Ti.API.info('fired blur event');

}); 
 
function parse(json)
{
	var data = [];
	var totalOffs = json.HotelOffers['@attributes'].totalOffers;
	createHeaderRow(totalOffs)
	var Hotels = json.HotelOffers.HotelOffer;
	Ti.API.info("Length: " + Hotels.length);
	
	for (var i = 0; i < Hotels.length; i++) 
	{
		
		var HotelName = json.HotelOffers.HotelOffer[i]['@attributes'].HotelName;
		var HotelImage = json.HotelOffers.HotelOffer[i]['@attributes'].MainImgURI;
		var duration = json.HotelOffers.HotelOffer[i]['@attributes'].PkgDuration;
		var rating = json.HotelOffers.HotelOffer[i]['@attributes'].Stars;
		var destination = json.HotelOffers.HotelOffer[i]['@attributes'].DestinationCatalog;
		var deptApt = json.HotelOffers.HotelOffer[i].Flights.OutboundFlight.DepartureAirport;
		var PricePP = json.HotelOffers.HotelOffer[i].Rooms.Room.PriceInfo.PriceBreakdown.AdultPrice['@attributes'].Amount;
		var lengthPrice = PricePP.length - 2;
		var GBPPrice = PricePP.substring(0,lengthPrice);
	
		createRow(HotelName,HotelImage,duration,rating,destination,deptApt,PricePP,lengthPrice,GBPPrice)	
	
	}	
}	

function createHeaderRow(totalOffs){
	
	data = [];
	var row = Ti.UI.createTableViewRow();
	row.backgroundColor = '#576996';
	row.selectedBackgroundColor = '#385292';
	row.height = 40;
	
	var clickLabel = Titanium.UI.createLabel({
		text:totalOffs+' holidays meet your search requirements',
		color:'#fff',
		textAlign:'center',
		font:{fontSize:14},
		width:'auto',
		height:'auto'
	});
	row.className = 'header';
	row.add(clickLabel);
	data.push(row);
	
	tableView.setData(data);
}

function createRow(HotelName,HotelImage,duration,rating,destination,deptApt,PricePP,lengthPrice,GBPPrice){		
	
		var nrow = Ti.UI.createTableViewRow();
		nrow.selectedBackgroundColor = '#4169e1';
		nrow.height = 100;
		nrow.className = 'datarow';
		nrow.clickName = 'row';
	
		var photo = Ti.UI.createImageView({
			defaultImage:'imgs/custom_tableview/user.png',
			image: 'http://www.thomascook.com'+HotelImage,
			top:5,
			left:5,
			width:100,
			height:100,
			clickName:'image'
		});
		nrow.add(photo);
		nrow.HotelImg = photo.image;
	
		var HotNameLabel = Ti.UI.createLabel({
			color:'#576996',
			font:{fontSize:16,fontWeight:'bold', fontFamily:'Arial'},
			left:110,
			top:2,
			height:30,
			width:180,
			text:''+HotelName
		});
	
		nrow.HotelName = HotNameLabel.text;
		nrow.add(HotNameLabel);
	
		var fontSize = 14;
	
		var comment = Ti.UI.createLabel({
			color:'#222',
			font:{fontSize:fontSize,fontWeight:'normal', fontFamily:'Arial'},
			left:110,
			top:21,
			height:50,
			width:180,
			//clickName:'comment',
			text:duration+' Nights,'+rating+' star hotel, from '+deptApt+' to '+destination+'.'
		});
		nrow.add(comment);
	
		var calendar = Ti.UI.createView({
			backgroundImage:'imgs/custom_tableview/pricetag.png',
			bottom:2,
			left:110,
			width:32,
			//clickName:'calendar',
			height:32
		});
		nrow.add(calendar);
	
		var button = Ti.UI.createView({
			backgroundImage:'imgs/custom_tableview/commentButton.png',
			top:35,
			right:5,
			width:36,
			//clickName:'button',
			height:34
		});
		nrow.add(button);
	
		var date = Ti.UI.createLabel({
			color:'#999',
			font:{fontSize:13,fontWeight:'normal', fontFamily:'Arial'},
			left:145,
			bottom:5,
			height:20,
			width:120,
			//clickName:'date',
			text:'from GBP '+GBPPrice+' p.p.'
		});
		nrow.add(date);
	    data.push(nrow);	
	    tableView.setData(data);
	   	
}